package com.cxb.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.telephony.TelephonyManager;

import com.cxb.manager.PhoneManager;

/**
 * 
 * @{# SharedPreferencesUtils.java Create on 2012-12-27 下午6:19:28
 * 
 *     类功能说�? SharedPreferences 保存应用的所有的配置信息
 * @Version 1.0
 * 
 * 
 */
public class SharedPreferencesUtils {
	private SharedPreferences sp;
	private SharedPreferences.Editor editor;
	private Context mContext;

	public SharedPreferencesUtils(Context context) {
		this.mContext = context;
		sp = mContext.getSharedPreferences(Constants.LEADERCXB_PREFERENCES,
				Context.MODE_PRIVATE);
		editor = sp.edit();
	}

	public void setGsensorAccCount(int count) {
		editor.putInt("acc_count", count);
		editor.commit();
	}


	public void setWeatherEffective(boolean effective) {
		editor.putBoolean("effective", effective);
		editor.commit();
	}

	public boolean isWeatherEffective() {
		return sp.getBoolean("effective", false);
	}

	public int getGsensorAccCount() {
		return sp.getInt("acc_count", 0);
	}

	public void setGsensorDecCount(int count) {
		editor.putInt("dec_count", count);
		editor.commit();
	}

	public int getGsensorDecCount() {
		return sp.getInt("dec_count", 0);
	}

	public void setGsensorTurnLeftCount(int count) {
		editor.putInt("turn_left_count", count);
		editor.commit();
	}

	public int getGsensorTurnLeftCount() {
		return sp.getInt("turn_left_count", 0);
	}

	public boolean getSetUpEdogWhenBootUp() {
		return false;
		// return sp.getBoolean("edog_setup", true);
	}

	public void setSetUpEdogWhenBootUpt(boolean enable) {
		editor.putBoolean("edog_setup", enable);
		editor.commit();
	}

	public int getVolumn() {
		return sp.getInt("volumn", 80);
	}

	public void setVoulmn(int volumn) {
		editor.putInt("volumn", volumn);
		editor.commit();
	}

	public int getLastVolumn() {
		return sp.getInt("last_volumn", 80);
	}

	public void setLastVoulmn(int volumn) {
		editor.putInt("last_volumn", volumn);
		editor.commit();
	}

	public int getBrightness() {
		return sp.getInt("brightness", 90);
	}

	public void setBrightness(int volumn) {
		editor.putInt("brightness", volumn);
		editor.commit();
	}

	public void setGsensorTurnRightCount(int count) {
		editor.putInt("turn_right_count", count);
		editor.commit();
	}

	public int getGsensorTurnRightCount() {
		return sp.getInt("turn_right_count", 0);
	}

	public float getFMChannel() {
		return sp.getFloat("FMChannel", 94.3f);
	}

	public void setFMChannel(float value) {
		editor.putFloat("FMChannel", value);
		editor.commit();
	}

	public void setWifiApEnable(boolean wifi) {
		editor.putInt("wifi_ap", wifi ? 1 : 0);
		editor.commit();
	}

	public boolean getWifiApEnable() {
		return sp.getInt("wifi_ap", 0) == 1;
	}

	public void setWifiRemain(boolean enable) {
		editor.putInt("wifi_remain", enable ? 1 : 0);
		editor.commit();
	}

	public boolean getWifiRemain() {
		return sp.getInt("wifi_remain", 0) == 1;
	}

	public void setFirstUseBaidu(boolean isFirst) {
		editor.putInt("baiduMap", isFirst ? 1 : 0);
		editor.commit();
	}

	public boolean isFirstUseBaidu() {
		return sp.getInt("baiduMap", 1) == 1;
	}

	public void setIsAutoLight(boolean isAuto) {
		editor.putBoolean("is_auto_light", isAuto);
		editor.commit();
	}

	public boolean isAutoLight() {
		return sp.getBoolean("is_auto_light", true);
	}

	public void setDevelopmentMode(boolean enable) {
		editor.putBoolean("development_mode", enable);
		editor.commit();
	}

	public boolean isDevelopmentMode() {
		return sp.getBoolean("development_mode", false);
	}

	public void setLanLo(double latitude, double longitude) {
		editor.putString("longitude", Double.toString(longitude));
		editor.putString("latitude", Double.toString(latitude));
		editor.commit();
	}

	public double getLatitude() {
		String las = sp.getString("latitude", "0.0");
		return Double.parseDouble(las);
	}

	public double getLongitude() {
		String los = sp.getString("longitude", "0.0");
		return Double.parseDouble(los);
	}

	/**
	 * @Title: getCallNumber
	 * @Description: 获取呼叫中心号码
	 * @return
	 * @date 2013-9-30 上午10:28:01
	 * @author 陈伟斌
	 */
	public String getCallNumber() {

		return sp.getString(Constants.LEADERCXB_CALLNUMBER, "0791116114");

	}

	/**
	 * @Title: setCallNumber
	 * @Description: 设置呼叫中心号码
	 * @param s
	 *            呼叫中心号码
	 * @date 2013-9-30 上午10:28:29
	 * @author 陈伟斌
	 */
	public void setCallNumber(String s) {
		editor.putString(Constants.LEADERCXB_CALLNUMBER, s);
		editor.commit();
	}

	/**
	 * @Title: getLocationNumber
	 * @Description: 获取本机号码
	 * @return
	 * @date 2013-9-30 上午10:42:06
	 * @author 陈伟斌
	 */
	public String getLocalNumber() {
		TelephonyManager telephoneyManager = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (telephoneyManager == null) {
			return null;
		}
		String ownNumber = telephoneyManager.getLine1Number();
		if (ownNumber == null) {
			ownNumber = "";
		}
		if (ownNumber.startsWith("+86")) {
			ownNumber = ownNumber.replace("+86", "");
		}

		if (ownNumber == null || ownNumber.length() < 1) {
			ownNumber = sp.getString(Constants.LEADERCXB_LOCATIONNUMBER, "");
		}

		// // /add by eric 20140327
		// if (!sp.getString(Constants.LEADERCXB_LOCATIONNUMBER, "").equals(
		// ownNumber)
		// && !sp.getString(Constants.LEADERCXB_LOCATIONNUMBER, "")
		// .equals("")) {
		// // 说明号码改变了
		// //
		// // // 通知梦挚地图
		// // Intent intent = new Intent("PHONENUM_CHANGED");
		// // intent.putExtra("phonenum", OwnNumber);
		// // mContext.sendBroadcast(intent);
		// // // 重新注册
		// new PhoneManager(mContext).registerDevice();
		// }
		editor.putString(Constants.LEADERCXB_LOCATIONNUMBER, ownNumber);
		editor.commit();
		// setLocalNumber(OwnNumber);

		return ownNumber;
	}

	/**
	 * @Title: setLocationNumber
	 * @Description: 设置本机号码
	 * @param s
	 * @date 2013-9-30 上午10:42:42
	 * @author 陈伟斌
	 */
	public void setLocalNumber(String s) {
		editor.putString(Constants.LEADERCXB_LOCATIONNUMBER, s);
		editor.commit();

		// // 通知梦挚地图
		// Intent intent = new Intent("PHONENUM_CHANGED");
		// intent.putExtra("phonenum", s);
		// mContext.sendBroadcast(intent);
		// 重新注册
		// new PhoneManager(mContext).registerDevice(s);
	}

	public void setIMSI(String imsi) {
		editor.putString(Constants.LEADERCXB_IMSI, imsi);
		editor.commit();
	}

	public String getIMSI() {
		return sp.getString(Constants.LEADERCXB_IMSI, "");
	}

	/**
	 * @Title: getPickproofNumber
	 * @Description: 获取防盗号码
	 * @return
	 * @date 2013-9-30 上午10:45:14
	 * @author 陈伟斌
	 */
	public String getPickproofNumber() {
		return sp.getString(Constants.LEADERCXB_PICKPROOFNUMBER, "");
	}

	/**
	 * @Title: setPickproofNumber
	 * @Description: 设置防盗号码
	 * @param s
	 * @date 2013-9-30 上午10:45:28
	 * @author 陈伟斌
	 */
	public void setPickproofNumber(String s) {
		editor.putString(Constants.LEADERCXB_PICKPROOFNUMBER, s);
		editor.commit();
	}

	public boolean getPickproofState() {
		return sp.getBoolean(Constants.LEADERCXB_PICKPROOFSTATE, false);
	}

	public void setPictproofState(boolean bState) {
		editor.putBoolean(Constants.LEADERCXB_PICKPROOFSTATE, bState);
		editor.commit();
	}

	/**
	 * @Title: get4SNumber
	 * @Description: 获取4s号码
	 * @return
	 * @date 2013-9-30 上午11:00:12
	 * @author 陈伟斌
	 */
	public String getSOSNumber() {
		return sp.getString(Constants.LEADERCXB_4SNUMBER, "");
	}

	/**
	 * @Title: set4SNumber
	 * @Description: 设置4s号码
	 * @param s
	 * @date 2013-9-30 上午11:00:41
	 * @author 陈伟斌
	 */
	public void setSOSNumber(String s) {
		editor.putString(Constants.LEADERCXB_4SNUMBER, s);
		editor.commit();
	}

	/**
	 * @Title: getWifiApSSID
	 * @Description: 获取wifiap SSID
	 * @return
	 * @date 2013-9-30 上午11:23:03
	 * @author 陈伟斌
	 */
	public String getWifiApSSID() {
		return sp.getString(Constants.LEADERCXB_WIFIAP_SSID, "AutoDriver");
	}

	/**
	 * @Title: setWifiApSSID
	 * @Description: 设置Wifiap SSID
	 * @param s
	 * @date 2013-9-30 上午11:24:03
	 * @author 陈伟斌
	 */
	public void setWifiApSSID(String s) {
		editor.putString(Constants.LEADERCXB_WIFIAP_SSID, s);
		editor.commit();
	}

	/**
	 * @Title: getWifiApPassword
	 * @Description: 获取wifiap 密码
	 * @return
	 * @date 2013-9-30 上午11:25:28
	 * @author 陈伟斌
	 */
	public String getWifiApPassword() {
		return sp.getString(Constants.LEADERCXB_WIFIAP_PASSWORD, "WXa12c34g");
	}

	/**
	 * @Title: setWifiApPassword
	 * @Description: 设置wifiap 密码
	 * @param s
	 * @date 2013-9-30 上午11:25:10
	 * @author 陈伟斌
	 */
	public void setWifiApPassword(String s) {
		editor.putString(Constants.LEADERCXB_WIFIAP_PASSWORD, s);
		editor.commit();
	}

	/**
	 * @Title: getTimeout
	 * @Description: 获取屏幕超时时间
	 * @return
	 * @date 2013-9-30 上午11:12:40
	 * @author 陈伟斌
	 */
	public int getTimeout() {
		return sp.getInt(Constants.LEADERCXB_TIMEOUT,
				Constants.Setting.SCREEN_TIMEOUT_DELAY);
	}

	/**
	 * @Title: setTimeout
	 * @Description: 设置超时时间
	 * @param i
	 * @date 2013-9-30 上午11:00:41
	 * @author 陈伟斌
	 */
	public void setTimeout(int i) {
		editor.putInt(Constants.LEADERCXB_TIMEOUT, i);
		editor.commit();
	}

	/**
	 * @Title: getTotalDistance
	 * @Description: 获取总里�?
	 * @return
	 * @date 2013-9-30 下午1:59:54
	 * @author 陈伟斌
	 */
	public int getTotalDistance() {
		return sp.getInt(Constants.LEADERCXB_TOTAL_DISTANCE, 0);
	}

	/**
	 * @Title: setTotalDistance
	 * @Description: 保存总里�?
	 * @param i
	 * @date 2013-9-30 下午1:59:32
	 * @author 陈伟斌
	 */
	public void setTotalDistance(int i) {
		editor.putInt(Constants.LEADERCXB_TOTAL_DISTANCE, i);
		editor.commit();
	}

	/**
	 * @Title: getIsSetupWizard
	 * @Description: 是否完成快速设�?
	 * @return
	 * @date 2013-11-7 下午5:05:23
	 * @author 陈伟斌
	 */
	public boolean getIsSetupWizard() {
		return sp.getBoolean(Constants.LEADERCXB_IS_SETUPWIZARD, false);
	}

	/**
	 * @Title: setIsSetupWizard
	 * @Description: 是否完成快速设�?
	 * @param b
	 * @date 2013-11-7 下午5:05:52
	 * @author 陈伟斌
	 */
	public void setIsSetupWizard(boolean b) {
		editor.putBoolean(Constants.LEADERCXB_IS_SETUPWIZARD, b);
		editor.commit();
	}

	/**
	 * @Title: getIsSetupWizard
	 * @Description: 是否完成快速设�?
	 * @return
	 * @date 2013-11-7 下午5:05:23
	 * @author 陈伟斌
	 */
	public boolean getIsSetupWizardWeiXin() {
		return sp.getBoolean(Constants.LEADERCXB_IS_SETUPWIZARDWEIXIN, false);
	}

	/**
	 * @Title: setIsSetupWizard
	 * @Description: 是否完成快速设�?
	 * @param b
	 * @date 2013-11-7 下午5:05:52
	 * @author 陈伟斌
	 */
	public void setIsSetupWizardWeiXin(boolean b) {
		editor.putBoolean(Constants.LEADERCXB_IS_SETUPWIZARDWEIXIN, b);
		editor.commit();
	}

	public boolean getRadarState() {
		return sp.getBoolean(Constants.LEADERCXB_RADAR_STATE, true);
	}

	public void setRadarState(boolean bOpened) {
		editor.putBoolean(Constants.LEADERCXB_RADAR_STATE, bOpened);
		editor.commit();
	}

	public void setAssiatePhoneNumber(String phone) {
		editor.putString("asscicate", phone);
		editor.commit();
	}

	public String getAssiatePhoneNumber() {

		return sp.getString("asscicate", "");
	}

	public boolean getAssiateState() {
		return sp.getBoolean("asscicate_state", false);
	}

	public void setAssiateState(boolean state) {
		editor.putBoolean("asscicate_state", state);
		editor.commit();
	}

	public void setStringWeather(String weather, long lastModified) {
		editor.putString("Stringweather", weather);
		editor.putLong("lastModifiedweather", lastModified);
		editor.commit();
	}

	public String getStringWeather() {
		if (System.currentTimeMillis() - sp.getLong("lastModifiedweather", 0) > 6 * 1000 * 60 * 60) {
			return null;
		}
		return sp.getString("Stringweather", null);
	}

	public boolean hasBoundRecord() {
		return sp.getBoolean("bound_record", false);
	}

	public void setBoundRecord(boolean isRecord) {
		editor.putBoolean("bound_record", isRecord);
		editor.commit();
	}

	public void setVirtualBtnPostion(Point point) {
		editor.putInt("virtual_btn_x", point.x);
		editor.putInt("virtual_btn_y", point.y);
		editor.commit();
	}

	public Point getVirtualBtnPostion() {
		int x = sp.getInt("virtual_btn_x", 60);
		int y = sp.getInt("virtual_btn_y", 80);
		Point point = new Point(x, y);
		return point;
	}
	
	public boolean getWeixinBind() {
		return sp.getBoolean("weixin_bind", false);
	}

	public void setWeixinBind(boolean bind) {
		editor.putBoolean("weixin_bind", bind);
		editor.commit();
	}
}